'''
Created on 16/10/2011

@author: nacho
'''

from mmu import MMU
#from assignation import Assignation 
from assignationFirstAdjustment import FirstAdjustment
#from assignationWorstAdjustment import WorstAdjustment
#from assignationBestAdjustment import BestAdjustment
from program import Program
from cpuInstruction import CPUInstruction
from pagination import Pagination




class Memory(object):
    '''
    classdocs
    '''


    def __init__(self,size):
        '''
        Constructor
        '''
        self.memory = {}
        self.memorySize=size
        
        
    def load(self, direction, instr):
        
        
        self.memory[direction] = instr
        
    def unload(self, direction):
        
        del self.memory[direction]
        
    def read(self, direction):
        
        try:
            self.memory[direction]
        except KeyError:
            return None
        else:
            return self.memory[direction]
        
        
        
    def size(self):
        
        return self.memorySize
    
    
if __name__ == '__main__':
    
    def printeable(cpu, str = "Higher"):
        
        print(str)
        
    def printeable2(cpu):
        
        print("Order")
        
        
    def defineVariable1(cpu):
        
        cpu.setVariable("x", 5)
        print("X variable setted")
        
    def defineVariable2(cpu):
        
        cpu.setVariable("y", 6)
        print("Y variable setted")
        
        
    def getVariableResult(cpu):
        
        x = cpu.getVariable("z")
        print(x)
        
    def sumVariables(cpu):
        
        x = cpu.getVariable("x")
        y = cpu.getVariable("y")
        cpu.setVariable("z", x + y)
        print(cpu.getVariable("z"))
    
    
    cpuvarx = CPUInstruction(printeable, 13)
    cpuvary = CPUInstruction(printeable2, 2)
    cpuvarz = CPUInstruction(printeable, 4)
    cpuvarw = CPUInstruction(printeable, 10)
    cpuvarz = CPUInstruction(printeable2, 8)
    cpusumvar = CPUInstruction(sumVariables)
    cpuresult = CPUInstruction(getVariableResult)
    #cpui2 = CPUInstruction(3)
    #cpui3 = CPUInstruction()
    program = Program([cpuvarx, cpuvary],1)
    settingvariables2 = Program([cpuvarw, cpuvarz],2)
    settingresult = Program([cpuvarz], 4)
    gettingresult = Program([cpuresult],1)
    #program3 = Program([cpui3],5)
    adj = FirstAdjustment()
    
    
    memory = Memory(512)
    allo = Pagination(4,memory)
    mmu = MMU(allo, memory)
    
    print(memory.memory)
    
    mmu.load(0, program.fullCode())
    print(memory.memory)
    
    #mmu.unload(0)
    #print(memory.memory)
    
    print(mmu.read(0, 8))
    
    
    
    